package edgeEnv

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/OS"
)

type edgeEnv struct {
	cpuId    string
	arch     string
	envType  string
	workMode string
}

func (e *edgeEnv) initialize() {
	// envType
	arg, ok := commEnv.GetArg("envType")
	if ok {
		e.envType = arg.(string)
	}
	if e.envType == "" {
		e.envType = "device"
	}

	// workMode
	arg, ok = commEnv.GetArg("workMode")
	if ok {
		e.workMode = arg.(string)
	}
	if e.workMode == "" {
		e.workMode = "local"
	}

	// cpuId
	arg, ok = commEnv.GetArg("envCpuId")
	if ok {
		e.cpuId = arg.(string)
	}
	if e.cpuId == "" {
		if OS.Hardware.SerialNumber != "" {
			e.cpuId = OS.Hardware.SerialNumber
		}
	}

	// arch
	e.arch = OS.Hardware.ARCH
}

func GetEdgeInfo() map[string]interface{} {
	data := make(map[string]interface{})

	diskInfo := OS.Sys.ReadDiskInfo()
	memInfo := OS.Sys.ReadMemInfo()
	cpuInfo := OS.Sys.ReadCpuInfo()

	data["cpuUID"] = env.cpuId
	data["envType"] = env.envType
	data["arch"] = env.arch

	data["diskSizeTxt"] = diskInfo["sizeTxt"]
	data["diskSize"] = Map.GetOrDefault(diskInfo, "size", 1)
	data["diskUsed"] = Map.GetOrDefault(diskInfo, "used", 1)
	data["diskAvail"] = Map.GetOrDefault(diskInfo, "avail", 1)
	data["diskUsePercentage"] = Map.GetOrDefault(diskInfo, "usePercentage", 1)

	data["ramTotalTxt"] = Map.GetOrDefault(memInfo, "ramTotalTxt", "")
	data["ramTotal"] = Map.GetOrDefault(memInfo, "ramTotal", 1)
	data["ramUsed"] = Map.GetOrDefault(memInfo, "ramUsed", 1)
	data["ramFree"] = Map.GetOrDefault(memInfo, "ramFree", 1)

	data["swapTotalTxt"] = Map.GetOrDefault(memInfo, "swapTotalTxt", "")
	data["swapTotal"] = Map.GetOrDefault(memInfo, "swapTotal", 1)
	data["swapUsed"] = Map.GetOrDefault(memInfo, "swapUsed", 0)
	data["swapFree"] = Map.GetOrDefault(memInfo, "swapFree", 1)

	data["cpuUs"] = Map.GetOrDefault(cpuInfo, "us", 1)
	data["cpuSy"] = Map.GetOrDefault(cpuInfo, "sy", 1)
	data["cpuNi"] = Map.GetOrDefault(cpuInfo, "ni", 1)
	data["cpuId"] = Map.GetOrDefault(cpuInfo, "id", 1)
	data["cpuWa"] = Map.GetOrDefault(cpuInfo, "wa", 1)
	data["cpuHi"] = Map.GetOrDefault(cpuInfo, "hi", 1)
	data["cpuSi"] = Map.GetOrDefault(cpuInfo, "si", 1)
	data["cpuSt"] = Map.GetOrDefault(cpuInfo, "st", 1)

	return data
}
